<template>
	<view class="container">
		<navigationBar :titleName="'个人资料'"></navigationBar>
		<view class="content setFontColor1">
			<view class="list">
				<view class="item sethorizontalbetween">
					<view class="name">
						头像
					</view>
					<u-avatar @click="swichImg()" size="100" :src="user.headImg" mode="square"></u-avatar>
				</view>
				<navigator v-if="user.phone" class="item" url="/pagesUser/bindMailbox?status=1&&page=2"
					hover-class="none">
					<view class="name">手机号码</view>
					
					<view class="go">
						<text class="value">{{ user.phone }}</text>
						<iconfont type="go"></iconfont>
					</view>
				</navigator>

				<view class="item">
					<view class="name">

						昵称
						
					</view>
					<view class="value settextRight">
					
						<input type="text"  v-model="user.showName" class="settextRight"/>
					</view>
				</view>



				<view class="item" v-if="emailType==2">
					<view class="name">修改邮箱</view>
					<view class="value">
						<input type="text" class="settextRight" v-model="user.email" placeholder="请填写邮箱" />
					</view>
				</view>
				<!-- v-if="user.email&&" -->
				<view class="item" v-if="emailType==2&&initEmail!=user.email">
					<view class="name">邮箱验证码</view>
					<view class="value">
						<input type="text" v-model="email_code" placeholder="请填写邮箱验证码" />
					</view>
					<view class="item" style="border: none;">
						<view class="btn-verify" @tap="getVerifyCode(1)">{{ verify_text }}</view>
					</view>
				</view>
				<navigator class="item" v-if="emailType==1" url="/pagesUser/bindMailbox?status=2" hover-class="none">
					<view class="name">添加邮箱</view>
					<text class="value"></text>
					<view class="go">
						<iconfont type="go"></iconfont>
					</view>
				</navigator>
				<navigator class="item" url="password" hover-class="none">
					<view class="name">修改密码</view>
					<text class="value"></text>
					<view class="go">
						<iconfont type="go"></iconfont>
					</view>
				</navigator>
				<!-- <view class="item" v-if="!user.phone">
					<view class="name">手机号码</view>
					<view class="value"><input placeholder="请输入手机号码" v-model="mobile" type="number" maxlength="11" />
					</view>
				</view>
				<view class="item" v-if="!user.phone">
					<view class="name">短信验证码</view>
					<input class="input" v-model="email_code" placeholder-class="tip" type="text" maxlength="6"
						placeholder="请输入验证码" />
					<view class="btn-verify" @tap="getVerifyCode()">{{ verify_text }}</view>
				</view> -->
			</view>
			<view class="submit">
				<view class="button" hover-class="button-hover" @tap="submit()">保存</view>
				<!-- <view class="btn-text" @tap="logout()">退出登录</view> -->
			</view>
		</view>
		<u-toast ref="uToast" />
		<pageLoading v-if="showPageLoading"></pageLoading>
	</view>
</template>

<script>
	import util from '@/common/util.js';
	import validator from '@/common/validator.js';
	import together from '@/common/together.js';
	import pageLoading from '@/components/loading/pageLoading.vue';
	import iconfont from '@/components/iconfont/iconfont.vue';

	let timing;
	export default {
		components: {
			pageLoading,
			iconfont
		},
		data() {
			return {
				// 邮箱验证码
				email_code: '',
				mobile: '',
				verify_code: '', //验证码
				second: 0,
				tempFilePath: '',
				cropFilePath: '',
				user: {

				},
				showPageLoading: true,
				// 保存用户原有邮箱
				initEmail: null,
				// 判断是显示 1添加邮箱  还是 2修改邮箱
				emailType: null

			};
		},
		onShow() {
			this.$initPageTitle(); //初始化页面标题
			let source = uni.getStorageSync('source');
			if (source == 'login') {
				uni.removeStorageSync('source');
				// #ifdef H5
				uni.navigateTo({
					url: '/pages/user/index'
				});
				// #endif

				// #ifndef H5
				uni.switchTab({
					url: '/pages/user/index'
				});
				// #endif
			}
			this.isbandPhone()
		},
		created() {
			// 全局可调用函数  用户数据刷新
			uni.$on('updateData', () => {
			
			
				this.loadData()
				
			})
			
		},
		
		onLoad(e) {

			this.loadData();

			// uni.$emit('updateData')

		},
		computed: {
			verify_text() {
				if (this.second == 0) {
					return '获取验证码';
				} else {
					if (this.second < 10) {
						return '0' + this.second + '秒后获取';
					} else {
						return this.second + '秒后获取';
					}
				}
			}
		},
		onPullDownRefresh() {
			uni.showLoading({
				title: '刷新中'
			});
			this.loadData();
		},
		methods: {
			// 更换头像图片
			swichImg() {
				uni.chooseImage({
					count: 1, // 图片数量
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album', 'camera'], //从相册选择或者拍照
					success: (res) => {
						const tempFilePaths = res.tempFilePaths;
						console.log(tempFilePaths[0])
						
						uni.uploadFile({
							url: 'https://caineng.com/caineng-api6/consumer/img/uploadZhaobiao', //上传图片api
							filePath: tempFilePaths[0],
							name: 'files',
							header: {
								'cainengtoken': uni.getStorageSync('accessToken'),
								'Content-type': 'application/x-www-form-urlencoded'
							},
							success: (res) => {
								let group = JSON.parse(res.data)
								this.user.headImg=group.data[0]
								//console.log("res++++++++++++++++++",res,group)
							}
						});
					}
				});

			},

			// 判断用户是否绑定了手机号
			isbandPhone() {
				// 判断用户是否绑定过手机号
				console.log("together.isBindPhone()", together.isBindPhone())
				if (!together.isBindPhone()) {
					// 用户未绑定好手机号  跳转至绑定页面
					uni.showModal({

						content: '您还未绑定手机号，请先绑定',
						success: function(res) {
							if (res.confirm) {
								// 跳转至手机绑定页面  
								uni.navigateTo({
									url: '/pagesUser/bindMailbox?status=1&&page=2'
								})
							} else if (res.cancel) {
								// 返回到上一界面
								uni.navigateBack()

							}
						}
					});
				}
			},
			// 信息提示
			showToast(data) {
				// 判断字符长度，长度大于7，用show，小于用alter
				if (data.length >= 7) {

					this.$refs.uToast.show({
						message: data,
						type: 'success'
					})
				} else {

					this.$alert(data);
				}

			},

			/*加载数据*/

			loadData() {

				this.getData();
			},

			/*获取数据*/
			getData() {
				this.$app.request({
					url: this.$baseApp.api.user.getUserInfo,
					data: {},
					method: 'GET',
					dataType: 'json',
					success: res => {
						if (res.code == 200) {
						
							this.user = res.data;
							if (this.user.email) {
								// 邮箱存在 修改
								this.emailType = 2
								this.initEmail = this.user.email
							} else {
								this.emailType = 1
							}

							this.mobile = this.user.mobile;
							this.cropFilePath = this.user.avatar_url;
							this.showPageLoading = false;
							
						} else {
							this.$alert(res.msg);
						}
					},
					complete: res => {
						uni.stopPullDownRefresh();
						uni.hideLoading();
					}
				});
			},


			getVerifyCode() {

				// 获取邮箱验证码
				// 验证邮箱
				if (!validator.checkEmail(this.user.email)) {
					// this.$alert('邮箱不正确')
					return;
				}

				this.$app.request({
					url: this.$baseApp.api.common.getEmailSms,
					data: {
						email: this.user.email
					},
					method: 'GET',
					dataType: 'json',
					success: res => {
						console.log("这里是获取邮箱验证码", res)
						// this.$alert(res.msg);
						this.showToast(res.msg)
						if (res.code == 200) {
							this.second = 300;
							timing = setInterval(() => {
								this.second--;
								if (this.second == 0) {
									clearInterval(timing);
								}
							}, 1000);
						}
					}
				});

			},


			/*保存*/
			submit() {

				// 用户修改邮箱
				if (this.initEmail != this.user.email && this.emailType == 2) {
					
					// 获取邮箱验证码
					// 验证邮箱
					if (!validator.checkEmail(this.user.email)) {
						// this.$alert('邮箱不正确')
						return;
					}
					if (!validator.checkVerifyCode(this.email_code)) {
						return;
					}
					this.$app.request({
						url: this.$baseApp.api.common.checkEmailSms,
						data: {
							email: this.user.email,
							code: this.email_code
						},
						method: 'GET',
						dataType: 'json',
						success: res => {
							console.log("邮箱验证码校验", res)
							// 校验失败
							if (res.code != 200) {

								// this.$alert(res.msg);
								this.showToast(res.msg)
								return
							} else {
								this.eixtdata()
							}
						}
					});
				} else {
					this.eixtdata()
				}
			},

			eixtdata() {
				uni.showLoading({
					title: '提交中…'
				});
				let data = {};
				data = {
					email: this.user.email || '',
					showName: this.user.showName,
					headImg:this.user.headImg
				};
				this.$app.request({
					url: this.$baseApp.api.user.updateInfo,
					data: data,
					method: 'POST',
					dataType: 'json',
					success: res => {
						if (res.code == 200) {
							this.$alert('保存成功', 'success');
						} else {
							this.$alert(res.msg, 'warning');
						}
						uni.navigateBack()
					},
					complete: res => {
						uni.hideLoading();
					}
				});
			},

			/*退出登录*/
			logout() {
				uni.showModal({
					title: '提示',
					content: '确认退出登录？',
					confirmText: '是',
					cancelText: '否',
					success: result => {
						if (result.confirm) {
							this.$app.request({
								url: this.$baseApp.api.user.logout,
								method: 'POST',
								success: res => {
									if (res.code == 200) {
										uni.removeStorageSync('isLogin');
										uni.removeStorageSync('accessToken');
										uni.removeStorageSync('currentUser');
										uni.removeStorageSync('platform');
										uni.setStorageSync('source', 'logout');

										uni.clearStorage(); //清空所有的缓存

										this.$alert('退出登录成功', 'success', '/pages/article/index',
											'switchTab');

									} else {
										this.$alert(res.msg, 'warning');
									}
								},
								complete: function() {
									uni.hideLoading();
								}
							});
						}
					}
				});
			}
		}
	};
</script>

<style lang="scss" scoped>
	.list {
		margin-top: 2rpx;
		padding-left: 24rpx;
		padding-right: 24rpx;
		border-bottom: 1rpx solid #eee;
		background: #fff;

		.item {
			display: flex;
			flex-direction: row;
			align-items: center;
			justify-content: space-between;
			min-height: 76rpx;
			border-top: 1rpx solid #eee;
			padding: 10rpx 0;

			&:first-child {
				border: 0;
			}

			.name {
				flex-grow: 0;
				flex-shrink: 0;
				display: flex;
				flex-direction: row;
				align-items: center;
				font-size: 30rpx;
				width: 200rpx;
				color: #555;

				/deep/ .icon {
					color: #0b88ff;
					margin-right: 10rpx;
				}

				/deep/ .icon-mobile-01 {
					font-size: 38rpx;
					margin-right: 5rpx;
					margin-left: -5rpx;
				}

				/deep/ .icon-policy-file {
					font-size: 30rpx;
					margin-right: 14rpx;
					margin-left: -2rpx;
					margin-top: 2rpx;
				}

				text {
					color: #555;
				}
			}

			.value {
				flex-grow: 1;
				flex-shrink: 1;
				display: flex;
				flex-direction: row;
				align-items: center;
				font-size: 30rpx;
				width: 100%;
				justify-content: flex-start;
				text-align: left;

				/deep/.icon {
					margin-left: 20rpx;
					font-size: 20rpx;
					color: #c1c4c9;
				}

				input {
					padding-right: 30rpx;
					font-size: 30rpx;
					width: 100%;
				}

				image {
					width: 60rpx;
					height: 60rpx;
					border-radius: 50%;
					margin-right: 10rpx;
				}

				text {
					margin-right: 15rpx;
					text-align: left;
					line-height: normal;
					padding: 10rpx 0 10rpx 0;
				}

				.nickname {
					font-size: 30rpx;
				}
			}

			.btn-verify {
				display: flex;
				justify-content: center;
				align-items: center;
				border: 1rpx solid #848c98;
				background: #fff;
				height: 46rpx;
				line-height: 46rpx;
				padding: 0 20rpx;
				border-radius: 55rpx;
				font-size: 26rpx;
				color: #999;
				width: 220rpx;
			}

			.tip {
				font-size: 30rpx;
				margin-right: 15rpx;
				color: grey;
			}

			.go {
				display: flex;
				flex-direction: row;
				align-items: center;
				justify-content: center;

				/deep/.icon {
					color: #dddddd;
					font-size: 20rpx;
				}
			}

			.location {
				display: flex;
				flex-direction: row;
				align-items: center;
				justify-content: center;

				/deep/.icon {
					color: grey;
				}
			}

			.drive-type {
				display: flex;
				align-items: center;

				.check-item {
					display: flex;
					align-items: center;
					margin-left: 30rpx;

					&:first-child {
						margin-left: 0;
					}

					.check-name {
						font-size: 30rpx;
						line-height: normal;
					}
				}
			}
		}

		.photo-item {
			height: 150rpx;

			image {
				width: 120rpx;
				height: 120rpx;
				border-radius: 50%;
			}
		}

		.textarea-item {
			padding-top: 10rpx;
			height: 110rpx;
			align-items: flex-start;

			.name {
				padding-top: 10rpx;
			}

			.value {
				textarea {
					padding-top: 12rpx;
					height: 100rpx;
					font-size: 30rpx;
					word-wrap: break-word;
					width: 524rpx;
					line-height: 1.5;
				}
			}
		}
	}

	.submit {
		padding: 30rpx 0 70rpx;
		background: #fff;

		.btn-text {
			display: flex;
			justify-content: center;
			margin-top: 24rpx;
			font-size: 32rpx;
			color: #8cc7b5;
		}
	}
</style>
